/*
    Copyright 2009 Barnet Wagman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package ot;

/** A <tt>Path</tt> uniquely identifies an object or directory
 *  within a {@link Context}.
 *
 * @author Barnet Wagman
 */
abstract public class Path 
     implements java.io.Serializable, Comparable {

    static final long serialVersionUID = ConstantsOt.UID_VERSION;

    public String[] pathnames;
  
    public Path(String[] pathnames) {
        this.pathnames = pathnames;
    }

    public boolean equals(Object other) {
        if ( !(other instanceof Path) ) return(false);
        Path o = (Path) other;
        if ( pathnames.length != o.pathnames.length ) return(false);
        for ( int i = 0; i < pathnames.length; i++ ) {
            if ( !pathnames[i].equals(o.pathnames[i]) ) return(false);
        }
        return(true);
    }

    public String toString() {
        StringBuilder s = new StringBuilder();
        for ( int i = 0; i < pathnames.length; i++ ) {
            s.append(pathnames[i]);
            if ( i < (pathnames.length-1) ) s.append("/");
        }
        return(s.toString());
    }

        /** The string is in
         *  standard '/' delimited string form, e.g.
         *  <tt>this/is/the/path/to/a/virtual/directory/</tt>.
         *  NOTE that the string has neither a leading nor a
         *  trailing '/'. A string in this form can
         *  be used as a 'prefix' in jets3t calls.
         */
    public String toPathString() {
        StringBuilder b = new StringBuilder(pathnames[0]);
        for ( int i = 1; i < pathnames.length; i++ )
            b.append("/" + pathnames[i]);
        return(b.toString());
    }


    public int compareTo(Object other) {
        Path o = (Path) other;
        return(this.toPathString().compareTo(other.toString()));
    }
}
